package com.shinechain.controller;

import com.shinechain.config.WebSecurityConfig;
import com.shinechain.dao.BotManagerMapper;
import com.shinechain.domain.DomainResp;
import com.shinechain.model.BotManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    BotManagerMapper botManagerMapper;

    @RequestMapping("*/login.html")
    public String index() {

        return "login.html";
    }

    @RequestMapping("/")
    public String goLogin(){
        return "login.html";
    }


    @RequestMapping(value = "login",method = RequestMethod.POST)
    @ResponseBody
    public DomainResp login(@RequestBody BotManager botManager, HttpSession session) {
        DomainResp domainResp = new DomainResp();
        BotManager myBotManager = botManagerMapper.selectByPrimaryKey(botManager.getId());
        if (myBotManager != null && myBotManager.getPassword().equals(botManager.getPassword())) {
            session.setAttribute("account", WebSecurityConfig.SESSION_KEY);
            domainResp.setStatus(200);
            return domainResp;
        } else {
            domainResp.setStatus(400);
            domainResp.setErrorMsg("密码或账号错误");
            return domainResp;
        }
    }
}
